const app = getApp()
const WXAPI = require("../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qyLs: [],
    assetId: '',
    info: {},
    basePowers: [], //基础权益
    addPowers: [], //附加权益
    vouchLs: [], //券列表
    activitys: [], //充值活动列表
    imgHttp: app.globalData.imgHttp, //图片前缀
    giveGoods: [],
    activitysLs: [],
    merchantId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom
    })
    this.data.assetId = options.assetId
    this.data.merchantId = options.merchantId
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  getDetail() {
    wx.showLoading({
      title: '加载中',
    })
    var data = {
      assetId: this.data.assetId,
      merchantId: this.data.merchantId
    }
    WXAPI.customerCardDetail(data, (res) => {
      if (res.code == 200) {
        this.data.basePowers = res.result.basePowers
        this.data.addPowers = res.result.addPowers
        this.data.qyLs = this.data.basePowers.concat(this.data.addPowers)
        this.data.activitys = res.result.activitys
        if (res.result.cardType == 5) {
          this.data.activitysLs = res.result.assetCardExperienceDetailDTOList
        } else {
          this.data.activitysLs = res.result.assetCardNumberDetailDTOList
        }
        if (res.result.chargeGiveDTOList.length > 1 && res.result.chargeGiveDTOList[0].powerName == "赠送金额") {
          this.data.giveGoods = res.result.chargeGiveDTOList.splice(0, 1);
        }
        this.setData({
          info: res.result,
          giveGoods: this.data.giveGoods,
          qyLs: this.data.qyLs,
          activitys: this.data.activitys,
          activitysLs: this.data.activitysLs,
        })
        wx.hideLoading()
      }
    })
  },
  //查看合同
  handleContract(e) {
    var url = this.data.imgHttp + e.currentTarget.dataset.url
    var index = url.lastIndexOf("\.");
    var type = url.substring(index + 1, url.length);
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    wx.downloadFile({
      url: url,
      header: {},
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          fileType: type,
          success: function (res) {
            console.log('打开文档成功')
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
        wx.hideLoading()
      },
      fail: function (res) {
        console.log('文件下载失败');
        wx.hideLoading()
      },
      complete: function (res) {},
    })

  },
  back() {
    wx.navigateBack({
      delta: 0,
    })
  },
  jumpShop(e) { //跳转店铺详情
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId=' + id,
    })
  },
})